import 'package:flutter/material.dart';
import 'package:inmotion_pro/config/const.dart';
import 'package:inmotion_pro/config/theme.dart';

import '../../../../utils/data_format_util.dart';
import 'custom_rich_text.dart';

///
/// 底部弹出选择列表
///
class DialogSelectListView extends StatelessWidget {
  /// 是否显示选中效果
  final bool? showSelect;

  /// 默认选中下标
  final int? selectedIndex;
  final String? more;
  final List<DialogSelectModel>? dataList;
  final void Function(int index)? onTap;
  final void Function()? onTapMore;

  const DialogSelectListView(
      {Key? key,
      this.showSelect = true,
      this.selectedIndex,
      this.more,
      required this.dataList,
      required this.onTap,
      this.onTapMore})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: lxColorScheme.blockColor,
      child: ListView.separated(
        physics: const BouncingScrollPhysics(),
        itemCount: dataList!.length + 1,
        padding: const EdgeInsets.only(bottom: 20),
        shrinkWrap: true,
        itemBuilder: (BuildContext context, int index) {
          if (index == dataList!.length) {
            return Visibility(
                visible: more != null,
                child: TextButton(
                  style: ButtonStyle(
                      shadowColor:
                          MaterialStateProperty.all(Colors.transparent),
                      overlayColor:
                          MaterialStateProperty.all(Colors.transparent)),
                  onPressed: onTapMore,
                  child: Text(
                    more ?? '',
                    style: Theme.of(context).listTitleStyle,
                  ),
                ));
          }
          return DialogSelectListViewItem(
            model: dataList?[index],
            showSelect: showSelect,
            isSelected: index == selectedIndex,
            onTap: () {
              onTap!(index);
            },
          );
        },
        separatorBuilder: (BuildContext context, int index) {
          return const Divider(height: 1.0);
        },
      ),
    );
  }
}

class DialogSelectModel {
  /// 标题
  final String? title;

  /// 描述
  final String? description;

  /// 关键词列表
  final List<String>? descriptionLinkList;

  final void Function(int index)? onTap;

  /// 附加内容
  final Object? value;

  /// 是否已选中
  final bool? check;

  /// 底部的子widget
  final Widget? child;

  /// 子组件展开高度
  final double? childHeight;

  DialogSelectModel(
      {this.title,
      this.description,
      this.descriptionLinkList,
      this.onTap,
      this.value,
      this.check,
      this.child,
      this.childHeight});
}

class DialogSelectListViewItem extends StatelessWidget {
  final DialogSelectModel? model;
  final bool? showSelect;
  final bool? isSelected;

  /// 点击回调
  final void Function()? onTap;

  final bool checkVal = false;

  const DialogSelectListViewItem({
    super.key,
    required this.model,
    this.showSelect,
    this.isSelected,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: AnimatedContainer(
          padding:
              EdgeInsets.only(top: 15, bottom: model!.child == null ? 15 : 5),
          height: getHeight(),
          constraints: const BoxConstraints(minHeight: rowHeight), // 最小高度
          duration: const Duration(milliseconds: 150),
          child: SizedBox(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        model!.title ?? '',
                        textAlign: TextAlign.left,
                        style: TextStyle(
                          fontSize: 15.0,
                          color: Theme.of(context).colorScheme.titleColor,
                        ),
                      ),
                      Visibility(
                          visible: model!.description != null,
                          child: Visibility(
                            visible: DataFormatUtils.isAvailableList(
                                model!.descriptionLinkList),
                            replacement: Container(
                              margin: const EdgeInsets.only(top: 5),
                              child: Text(model!.description ?? '',
                                  style: Theme.of(context).listSubtitleStyle),
                            ),
                            child: Container(
                              margin: const EdgeInsets.only(top: 5),
                              child: CustomRichText(
                                fullText: model!.description ?? '',
                                linkTextList: model!.descriptionLinkList ?? [],
                                onTap: model!.onTap,
                              ),
                            ),
                          )),
                      Visibility(
                          visible: model!.child != null && isSelected == true,
                          child: Expanded(
                              child: Container(
                                  margin: const EdgeInsets.only(top: 10),
                                  child: model!.child ??
                                      const SizedBox.shrink()))),
                    ],
                  ),
                ),
                // 选中图标
                Visibility(
                    visible: showSelect == true && isSelected == true,
                    child: Container(
                      alignment: Alignment.topRight,
                      child: Icon(
                        Icons.check,
                        color: Theme.of(context).colorScheme.titleColor,
                      ),
                    )),
              ],
            ),
          )),
      onTap: () {
        if (onTap != null) {
          onTap!();
        }
      },
    );
  }

  double? getHeight() {
    if (model!.child != null) {
      // 存在子组建，高度固定
      if (isSelected == true) {
        return rowHeight + (model!.childHeight ?? 40.0);
      } else {
        // 收起高度，如果存在详情文字则高度适当增加
        return rowHeight;
      }
    }
    // 没有子组件，自适应高度，返回null
    return null;
  }
}
